#include"../include/func.h"

void regisAndlogin_client(int clientfd)
{
    while(1)
    {
        train_t train;
        printf("input s(showtables) r(regis) l(login) e(exit):\n");
        char a=0;
        scanf("%c",&a);
        //fflush(stdin);
        getchar();

        //将收到的指令发送给服务器
        int ret=send(clientfd,&a,1,MSG_WAITALL);
        if(a=='e')
        {
            break;
        }
        if(a=='s')
        {
            continue;
        }
        if(a=='r')      //执行注册逻辑
        {
            printf("regist name:");
            fflush(stdout);
            //从键盘得到注册名字
            char registname[66]={0};
            scanf("%s",registname);
            //fflush(stdin);
            getchar();
            //小火车发送注册的名字给服务器
            memset(&train,0,sizeof(train));
            train.length=strlen(registname);
            strcpy(train.buff,registname);
            send(clientfd,&train,4+train.length,MSG_WAITALL);

            //接收服务器注册名判重 1成功 0失败
            int ret=9;
            recv(clientfd,&ret,4,MSG_WAITALL);
            if(ret==0)
            {
                continue;
            }
            /*执行密码注册*/
            
            //从键盘读入密码
            char passwd[66]={0};
            printf("%s passwd:",registname);
            fflush(stdout);
            scanf("%s",passwd);
            //fflush(stdin);
            getchar();
            //小火车发送密码
            memset(&train,0,sizeof(train));
            train.length=strlen(passwd);
            strcpy(train.buff,passwd);
            send(clientfd,&train,4+train.length,MSG_WAITALL);
            
            //打印注册成功信息
            printf("regist user:%s succeed!\n",registname);
            fflush(stdout);
            fflush(stdin);
        }

    }
}
